﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using Dimok;
	public static class ControlExtentions {
		#region LoadConptrol

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	A string extension method that loads a control. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="Path">	The Path to act on. </param>
		///
		/// <returns>	The control. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static string LoadControl( this string Path) { return LoadControl(Path, new Dictionary<string, object>()); }

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	A string extension method that loads a control. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="Path">				The Path to act on. </param>
		/// <param name="Properties">	The properties. </param>
		///
		/// <returns>	The control. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static string LoadControl(this string Path, object Properties) {
				// Create instance of the page control
				Page page = new Page();
				// Create instance of the user control
				UserControl userControl = (UserControl)page.LoadControl(Path);
				Properties.Copy(userControl);
				//Disabled ViewState- If required
				userControl.EnableViewState = false;
				HtmlForm form = new HtmlForm();
				form.Controls.Add(new ScriptManager());
				form.Controls.Add(userControl);
				page.Controls.Add(form);
				StringWriter textWriter = new StringWriter();
				HttpContext.Current.Server.Execute(page, textWriter, false);
				return CleanHtml(textWriter.ToString());
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Clean html. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="html">	The html. </param>
		///
		/// <returns>	. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		private static string CleanHtml(string html) {
			return Regex.Replace(html, @"<[/]?(form)[^>]*?>", "", RegexOptions.IgnoreCase);
		}
		#endregion
	}
